package com.christianposta.learnddd.domain.model.cargo;

import org.apache.commons.lang.Validate;

import com.christianposta.learnddd.domain.model.handling.HandlingHistory;
import com.christianposta.learnddd.domain.model.location.Location;
import com.christianposta.learnddd.domain.shared.Entity;

public class Cargo implements Entity<Cargo>{
	
	private TrackingId trackingId;
	private Location origin;
	private RouteSpecification routeSpecification;
	private Itinerary itinerary;
	private Delivery delivery;
	
	public Cargo(final TrackingId trackingId, final RouteSpecification routeSpecification){
		Validate.notNull(trackingId);
		Validate.notNull(routeSpecification);
		
		this.trackingId = trackingId;
		
		// It is possible for the route specification to change, however,
		// when a cargo is created, its origin cannot change.
		this.origin = routeSpecification.origin();
		
		this.routeSpecification = routeSpecification;
		
		// TODO Finish Delivery
		this.delivery = Delivery.derivedFrom(routeSpecification, itinerary, HandlingHistory.EMPTY);
	}

	/**
	 * As entities are compared by identity and not attributes/members this 
	 * method will compare a Cargo entity by its tracking id which is the 
	 * domain's understanding of how Cargo are differentiated. Other attributes
	 * are not necessary in making this distinction.
	 */
	public boolean sameIdentityAs(Cargo other) {
		return other != null && trackingId.sameValueAs(other.trackingId);
	}

}
